﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.Web" %>

<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="Mocola.Common"%>

<script RunAt="server">
    private static string LOG_SOURCE = ConfigurationManager.AppSettings["Event Log Source"];
    
    // If an exception is thrown in the application then log it to an event log
    protected void Application_Error(object sender, EventArgs e) {
        Exception x = Server.GetLastError().GetBaseException();
        EventLog.WriteEntry(LOG_SOURCE, x.ToString(), EventLogEntryType.Error);
    }

    void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
    {
        IPrincipal user = HttpContext.Current.User;

        if (user.Identity.IsAuthenticated && user.Identity.AuthenticationType == "Forms")
        {
            FormsIdentity formIdentity = user.Identity as FormsIdentity;
            CustomIdentity identity = new CustomIdentity(formIdentity.Ticket);

            CustomPrincipal principal = new CustomPrincipal(identity);
            HttpContext.Current.User = principal;

            Thread.CurrentPrincipal = principal;
        }
    }
</script>